data structures number theory *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ld long double
#define OK(x) cout<<(x?"YES\n":"NO\n")
#define ok(x) cout<<(x?"yes\n":"no\n")
#define Ok(x) cout<<(x?"Yes\n":"No\n")
#define lp(n) for(ll i = 0; i < (n); ++i)
#define lp1(n) for(ll i = 1; i <= (n); ++i)
#define watch(num) cout<< #num <<": "<< num << "\n";
#define watchV(v) cout<< #v <<": "; for(auto i:v) cout << i << " ";
#define en '\n'
#define Endl '\n'
#define endl '\n'
#define al(n) n.begin(), n.end()
#define all(n) n.rbegin(), n.rend()
#define GRAPH vector<vector<int>>
const ll MOD = 1000000007;
const ll OO = INT32_MAX;
#define speed ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define tc ll testcase;cin>>testcase;while(testcase--)

template<typename T>
istream &operator>>(istream &input, vector<T> &data) {
    for (T &x: data)input >> x;
    return input;
}

template<typename T>
ostream &operator<<(ostream &output, const vector<T> &data) {
    for (const T &x: data)output << x << " ";
    return output;
}

int numOfDigits(ll num) {
    return 1 + floor(log10(num));
}

ll tree_SZ = 1e5 + 5;
vector<ll> arr, tree;

void build_tree(ll node, ll left, ll right) {
    if (left == right) {
        tree[node] = arr[left];
        return;
    }
    ll mid = (left + right) / 2;

    build_tree(2 * node, left, mid);
    build_tree((2 * node) + 1, mid + 1, right);

    tree[node] = __gcd(tree[2 * node], tree[(2 * node) + 1]);

}

void update_tree(ll node, ll left, ll right, ll idx, ll val) {
    if (idx > right || idx < left)return;

    if (left == right) {
        tree[node] = val;
        return;
    }

    ll mid = (left + right) / 2;
    update_tree(2 * node, left, mid, idx, val);
    update_tree((2 * node) + 1, mid + 1, right, idx, val);

    tree[node] = __gcd(tree[2 * node], tree[(2 * node) + 1]);
}

int cnt;

void query(ll node, ll left, ll right, ll l, ll r, ll gcd) {
    if (r < left || l > right)return;
    if (l <= left && r >= right) {
        if (!(tree[node] % gcd))return;
        else {
            if (left == right) {
                cnt++;
                return;
            }
        }
    }

    ll mid = (left + right) / 2;
    query(2 * node, left, mid, l, r, gcd);
    if (cnt > 1)return;
    query((2 * node) + 1, mid + 1, right, l, r, gcd);
    if (cnt > 1)return;

}


void solve() {
    cin >> tree_SZ;
    tree.resize(4 * tree_SZ);
    arr.resize(tree_SZ + 1, 0);
    for (int i = 1; i <= tree_SZ; i++)cin >> arr[i];
    build_tree(1, 1, tree_SZ);
    int q;
    cin >> q;
    while (q--) {
        int x;
        cin >> x;
        if (x == 1) {
            int l, r, g;
            cin >> l >> r >> g;
            cnt = 0;
            query(1, 1, tree_SZ, l, r, g);
            OK(cnt <= 1);
        } else {
            int idx, val;
            cin >> idx >> val;
            update_tree(1, 1, tree_SZ, idx, val);
        }
    }

}

int main() {

    speed

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

//    tc
    solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

1438A - Specific Tastes of Andre
1711C - Color the Picture
1194C - From S To T
110B - Lucky String
1114A - Got Any Grapes
224B - Array
125B - Simple XML
567B - Berland National Library
431B - Shower Line
282C - XOR and OR
1582B - Luntik and Subsequences
609A - Флеш-карты
1207A - There Are Two Types Of Burgers
371C - Hamburgers
343B - Alternating Current
758B - Blown Garland
1681B - Card Trick
1592A - Gamer Hemose
493D - Vasya and Chess
1485A - Add and Divide
337B - Routine Problem
1392D - Omkar and Bed Wars
76E - Points
762C - Two strings
802M - April Fools' Problem (easy)
577B - Modulo Sum
1555B - Two Tables
1686A - Everything Everywhere All But One
1469B - Red and Blue
1257B - Magic Stick